var requiredFields = [ "firstname", "lastname", "email", "message" ];

function checkContactForm() {
  var theForm = document.forms[ 0 ];
  
  for ( var i in requiredFields ) {
    var fieldName = requiredFields[ i ];
    
    if ( !theForm[ fieldName ].value || theForm[ fieldName ].value == "Error" ) {
      theForm[ fieldName ].style.color = "#f66";
      theForm[ fieldName ].value = "Error";
      
      var emptyFields = true; 
      
    } 
  }
  
  
  
  if ( !emptyFields ) { theForm.submit();}
  else{
	  var errorDiv = document.createElement("div");
	  errorDiv.setAttribute("class","error");
	  errorDiv.setAttribute("id","error");
	  var errorIcon = document.createElement("img");
	  errorIcon.setAttribute("class","errorIcon");
	  errorIcon.setAttribute("src","images/erroricon.png");
	  errorDiv.appendChild(errorIcon);
	  errorDiv.innerHTML=errorDiv.innerHTML+"Please fill all fields before submitting the form";
	  document.getElementsByTagName("body")[0].appendChild(errorDiv);
  }
}

function resetField( theField ) {
  if ( theField.value == "Error" ) {
    theField.value = "";
  }
  theField.style.color = "#000";
  
 
}